package com.lightsh.handwritten;

import java.util.ArrayList;
import java.util.Arrays;

import com.lightsh.common.PaintItem;
import com.lightsh.common.PaintStroke;
import com.lightsh.db.DbDataHelper;
import com.lightsh.db.MyDbHelper;
import com.lightsh.paint.FingerPaintView;
import com.lightsh.utility.Constants;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class FingerPaintActivity extends Activity{
	private static final String TAG = "FingerPaintActivity";
	private static final boolean DEBUG = Constants.DEBUG;
	public static final String EXTRA_PAINT_ID = "ExtraPaintId";
	private FingerPaintView mPaintView;
	public static final int INVALID_ID = PaintItem.INVALID_ID;
    private long mId = INVALID_ID;
    private PaintItem mItem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		Bundle bundle = getIntent().getExtras();
		if(bundle != null){
			mId = bundle.getLong(EXTRA_PAINT_ID);
		}
		
		if(mId != INVALID_ID){
			mItem = DbDataHelper.getPaintInfo(this, mId);
			if(mItem == null){
				Toast.makeText(this, "paint not found id = " + mId + "!!!", Toast.LENGTH_LONG).show();
				this.finish();
				return;
			}
			mPaintView = new FingerPaintView(this, mItem);
		}else{
			int width = getWindowManager().getDefaultDisplay().getWidth();
			int height = getWindowManager().getDefaultDisplay().getHeight();
			mPaintView = new FingerPaintView(this, width, height);
		}
		setContentView(mPaintView);		
	}
	

	@Override
	public void onBackPressed() {
		checkAndSavePaint();
		super.onBackPressed();
	}


	@Override
	protected void onDestroy(){
		super.onDestroy();
	}
	private void checkAndSavePaint(){
		if(mPaintView == null){
			return;
		}
		ArrayList<PaintStroke> list = mPaintView.getPaintStrokes();		
		String text = mPaintView.getText();
		if(mId != INVALID_ID){
			if(!mPaintView.isChange()){
				return;
			}
		}else{
			if((text == null || text.length() == 0) && (list.size() == 0)){
				return;
			}
		}
		logD("save paint id = " + mId);
		if(DEBUG){
			Log.w(TAG, "stroke size = " + mPaintView.getPaintStrokes().size());
			for(PaintStroke stroke : mPaintView.getPaintStrokes()){
				Log.w(TAG, "stroke = " + Arrays.toString(stroke.getStrokePoints()));
			}
		}
		boolean bSuccess = DbDataHelper.savePaintInfo(this, mId, text, mPaintView.getPaintWidth(), mPaintView.getPaintHeight(), list);
		if(!bSuccess){
    		Toast.makeText(this, R.string.saveFail, Toast.LENGTH_LONG).show();
    		return;
    	}
    	setResult(Activity.RESULT_OK);
	}
	
	private void logD(String tip){
		if(DEBUG){
			Log.d(TAG, "" + tip);
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 1, getString(R.string.delete));
		return super.onCreateOptionsMenu(menu);
	}


	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.getItem(0).setEnabled(mPaintView.getStrokeSize() > 0);
		return super.onPrepareOptionsMenu(menu);
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case 0:
			mPaintView.popStroke();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}
